package com.net.module.controller;

import com.net.module.entity.Inventory;
import com.net.module.service.IInventoryService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * <p>
 * 库存表 前端控制器
 * </p>
 *
 * @author 猿仁
 * @since 2023-03-02
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/api/inventory")
public class InventoryController {

    @Autowired
    private IInventoryService service;

    @GetMapping("/getById/{id}")
    public Inventory getById(@PathVariable("id") Long id){
        return service.getById(id);
    }

    @PostMapping("/insert")
    public boolean getById(@RequestBody Inventory inventory){
        return service.save(inventory);
    }

    @GetMapping("/inventorySubOne/{id}")
    public boolean inventorySubOne(@PathVariable("id") Long id, HttpServletRequest httpServletRequest) {
        StringBuffer params = new StringBuffer();
        Enumeration<String> parameterNames = httpServletRequest.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String k = parameterNames.nextElement();
            String v = httpServletRequest.getParameter(k);
            params.append(String.format("%s=%s ", k, v));
        }
        log.info("xid:  {}" , httpServletRequest.getHeader(RootContext.KEY_XID));
        log.info("params:  {}" , params);
        return service.inventorySubOne(id);
    }

    @GetMapping("/getByBussName/{bussName}")
    public Inventory getByBussName(@PathVariable("bussName") String bussName){
        return service.getByBussName(bussName);
    }




}
